package com.fumin.quartz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fumin.pojo.InvestmentRecord;
import com.fumin.service.InvestmentRecordService;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 当证书过期定时发送邮件
 * 
 * @author aspireyc
 * 
 */
@Component
public class SendMailTask {

	@Resource
	InvestmentRecordService investmentRecordService;

	@Scheduled(cron = "0 00 00 * * ?")
	public void autoStatisticWxInfo() {
		try {
			List<InvestmentRecord> allDatas = investmentRecordService.selectAll();
			for (InvestmentRecord investmentRecord:allDatas) {
				String url = "https://api.etherscan.io/api?module=transaction&action=gettxreceiptstatus&txhash=" + investmentRecord.getTransaction_hash() + "&apikey=H6T8WITXFCYG189UEFV2FTNAGWZQIFWHQW";
				HttpResponse<String> response = Unirest.get(url)
						.asString();
				String body = response.getBody();

				JSONObject resuelt = JSON.parseObject(body);
				JSONObject result = resuelt.getJSONObject("result");
				String status = result.getString("status");
				if ("0".equals(status)){
					investmentRecord.setStatus(5);
					investmentRecordService.updateByPrimaryKeySelective(investmentRecord);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
}
